﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Entity;
using IRealize;
using Microsoft.Data.SqlClient;

namespace Realize
{
    public class Config_Major_Kind : IConfig_Major_Kind
    {
        public async Task<int> deleteCMK(string id, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"delete from config_major_kind where mfk_id={id}";
                return await con.ExecuteAsync(sql);
            }
        }

        public async Task<int> insertCMK(string name, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"insert into config_major_kind(major_kind_name) values('{name}')";
                return await con.ExecuteAsync(sql);
            }
        }

        public async Task<List<Entity.Config_Major_Kind>> selectCMK(string sqlserver)
        {

            Task<List<Entity.Config_Major_Kind>> task = Task<List<Entity.Config_Major_Kind>>.Run(async () =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {
                    string sql = "select * from config_major_kind";
                    return con.Query<Entity.Config_Major_Kind>(sql).ToList();
                }
                
            });
            return await task;
        }

        public async Task<int> updateCMK(string id, string name, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"update config_major_kind set major_kind_name='{name}' where mfk_id={id}";
                return await con.ExecuteAsync(sql);
            }
        }
    }
}
